package com.founder.helloworldapp;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.location.Location;
import android.util.AttributeSet;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class TrackMapView extends MapView {
	
	private List<Location> locations;
	
	private boolean alreadyCenterMap = false;
	
	public TrackMapView(Context context, String apikey) {
		super(context, apikey);
	}

	public TrackMapView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public TrackMapView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
    private void centerMap() {
    	if (locations == null || locations.size() < 1) {
    		return;
    	}
    	int maxLat=0, minLat=0, maxLng=0, minLng=0;
    	for (int i = 0; i < locations.size(); i++) {
    		Location l = locations.get(i);
    		int lat = (int)l.getLatitude();
    		int lng = (int)l.getLongitude();
    		if (i == 0) {
    			maxLat = lat;
    			minLat = lat;
    			maxLng = lng;
    			minLng = lng;
    		} else {
        		if (Math.abs(lat) > Math.abs(maxLat)) {
        			maxLat = lat;
        		}
        		if (Math.abs(lat) < Math.abs(minLat)) {
        			minLat = lat;
        		}
        		if (Math.abs(lng) > Math.abs(maxLng)) {
        			maxLng = lng;
        		}
        		if (Math.abs(lng) < Math.abs(minLng)) {
        			minLng = lng;
        		}
    		}
    	}
    	
    	int centerLat = (maxLat - minLat) / 2 + minLat;
    	int centerLng = (maxLng - minLng) / 2 + minLng;
		GeoPoint centerPoint = new GeoPoint(centerLat, centerLng);
		this.getController().setCenter(centerPoint);  
		this.getController().setZoom(20); 
		//Log.d("hello", "level=" + this.getZoomLevel() + ",centerPoint=" + centerPoint);
    	if (locations.size() > 2) {
    		GeoPoint point = this.getProjection().fromPixels(0, 0);
        	int pixLat = point.getLatitudeE6();
        	int pixLng = point.getLongitudeE6();
        	while (this.getZoomLevel() > 1 && minLng < pixLng) {
        		this.getController().setZoom(this.getZoomLevel() - 1);
        		point = this.getProjection().fromPixels(0, 0);
        		pixLat = point.getLatitudeE6();
        		pixLng = point.getLongitudeE6();
        		//Log.d("hello", "level=" + this.getZoomLevel() + ",point=" + point);
        	}
        	while (this.getZoomLevel() > 1 && maxLat > pixLat) {
        		this.getController().setZoom(this.getZoomLevel() - 1);
        		point = this.getProjection().fromPixels(0, 0);
        		pixLat = point.getLatitudeE6();
        		//Log.d("hello", "level=" + this.getZoomLevel() + ",point=" + point);
        	}
    	}
    	this.getController().animateTo(centerPoint);
	    
    }

	public void setLocations(List<Location> locations) {
		this.locations = locations;
		alreadyCenterMap = false;
		this.invalidate();
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		if (!alreadyCenterMap && (getHeight() > 0)) {
			centerMap();
			alreadyCenterMap = true;
			//Log.d("hello", "centerMap is run");
		}
	}
}
